﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;

public partial class AddProcess : System.Web.UI.Page
{
    public int sign = 0;
    public int flow_id = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        if (!Page.IsPostBack)
        {
            if (Request.QueryString["flow_id"] != null)
            {
                int flowId = int.Parse(Request.QueryString["flow_id"]);

                DAL.FlowDALs flowdals = new DAL.FlowDALs();
                Flow flow = flowdals.GetByID(flowId);
                Session["flow"] = flow;
                
                TxtFlowName.Text = flow.Name;
                TxtStartTime.Text = flow.Start_time;
                TxtEndTime.Text = flow.End_time;
                TxtPubStep.Text = flow.Release_setup;
                TxtPDAM.Text = flow.Risk;
                TxtShowCheckInfo.Text = flow.Check_message;

                this.DropFlowType.Items[this.DropFlowType.SelectedIndex].Selected = false;
                if (flow.Type == 1)
                {
                    this.DropFlowType.Items[0].Selected = true;
                }
                else
                    this.DropFlowType.Items[1].Selected = true;


                this.DropDownList1.Items[this.DropFlowType.SelectedIndex].Selected = false;
                if (flow.Status == 0)
                {
                    this.DropDownList1.Items[0].Selected = true;
                }
                else if (flow.Status == 1)
                    this.DropDownList1.Items[1].Selected = true;
                else if (flow.Status == 2)
                    this.DropDownList1.Items[2].Selected = true;

                DAL.Work_serverDALs work_serverdals = new DAL.Work_serverDALs();

                System.Collections.Generic.IList<Work_server> list1 = work_serverdals.GetByFlowId(flow.Flow_id);
                System.Collections.Generic.IList<Server> lst = new System.Collections.Generic.List<Server>();


                DAL.ServerDALs serverdals = new DAL.ServerDALs();

                this.ChkServerList.DataSource = serverdals.GetList();
                this.ChkServerList.DataBind();

                foreach (Work_server work_server in list1) 
                {
                    int server_id = work_server.Server_id;

                    foreach (ListItem item in this.ChkServerList.Items)
                    {
                        if (item.Value == server_id.ToString())
                        {
                            item.Selected = true;
                        }
                     }
                }

                cmdAdd2.Text = "修改";
                this.signtype.Value = "1";
                this.flowid.Value = flow.Flow_id.ToString();
                Session.Remove("flow");
            }
            else
            {
                DAL.ServerDALs server = new DAL.ServerDALs();
                this.ChkServerList.DataSource = server.GetList();
                this.ChkServerList.DataBind();
                if (sign == 0)
                {
                    this.Panel1.Visible = false;
                }
            }

        }
    }
    protected void cmdAdd2_Click(object sender, EventArgs e)
    {

        string username = Session["AdminName"].ToString() ;
        DAL.UserDALs userdals = new DAL.UserDALs();
        User user = userdals.GetByName(username);

        if (this.signtype.Value != "1") 
        {
            DAL.FlowDALs flow = new DAL.FlowDALs();
            int flow_id = flow.Add(this.TxtFlowName.Text, int.Parse(this.DropFlowType.SelectedValue),this.TxtStartTime.Text.Trim(), this.TxtEndTime.Text.Trim(),user.User_id, this.TxtPubStep.Text, this.TxtPDAM.Text);

            DAL.Work_serverDALs work_server = new DAL.Work_serverDALs();

            foreach (ListItem item in this.ChkServerList.Items)
            {
                if (item.Selected)
                {
                    work_server.Add(int.Parse(item.Value),flow_id);
                }
            }
        }
        else if (this.signtype.Value == "1")
        {
            DAL.FlowDALs flowdals = new DAL.FlowDALs();
            Flow flow = flowdals.GetByID(int.Parse(this.flowid.Value));

            flow.Check_user_id = user.User_id;
            flow.Check_message += TxtCheckInfo.Text;
           
            flow.End_time = TxtEndTime.Text;
            flow.Start_time = TxtStartTime.Text;
            flow.F1 = username;
            flow.Name = TxtFlowName.Text;
            flow.Release_setup = TxtPubStep.Text;
            flow.Risk = TxtPDAM.Text;

            flow.Type = int.Parse(this.DropFlowType.SelectedValue);
            flow.Status = short.Parse(this.DropDownList1.SelectedValue);

            flowdals.CreateUpdateDeleteFlow(flow, DAL.DataProviderAction.Update);


            DAL.Work_serverDALs work_serverdals = new DAL.Work_serverDALs();

            System.Collections.Generic.IList<Work_server> list1 = work_serverdals.GetByFlowId(int.Parse(this.flowid.Value));
            System.Collections.Generic.IList<String> lst = new System.Collections.Generic.List<String>();
           
            foreach (ListItem item in this.ChkServerList.Items)
            {
                if (item.Selected == true)
                {
                    lst.Add(item.Value);
                       
                }

            }

            foreach (Work_server work_server in list1)
            {
                work_serverdals.DeleteWork_server(work_server.Id);
            }

            foreach (string str in lst)
            {
                work_serverdals.Add(int.Parse(str), flow.Flow_id);
            }
           
        }
        Response.Redirect("ProcessList.aspx");
    }
}
